package com.acc.store.contorller;


import com.acc.store.common.Result;
import com.acc.store.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RequestMapping("/common")
@RestController
@Slf4j
@Tag(name = "通用操作")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public Result<String> upload(@RequestParam MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String objectname = UUID.randomUUID().toString() + extension;

        log.info("{}", file);
        String filepath = null;
        try {
            filepath = aliOssUtil.upload(file.getBytes(),"acc-store/"+objectname);
            log.info("上传到{}",filepath);
            return Result.success(filepath);
        } catch (IOException e) {
            log.error("上传失败{}",e);
        }
        return Result.error("上传失败");
    }
}